<?php

namespace app\api\controller;

use app\api\model\ResourceExtend;
use think\Controller;
use think\Db;

class Index extends Controller
{
    public function index()
    {
        return $this->fetch('index/index');
    }

    /**
     * 用户浏览页面
     */
    public function listing()
    {
        $where = ['t.is_delete' => 0];
        $__page_size = input('__page_size',10);

        $type_id = input('type_id',0);
        if ($type_id) {
            $where['t.type_id'] = $type_id;
        }

        $list = Db::name('t_resource')->alias('t')
            ->join('t_type type','t.type_id = type.id')
            ->where($where)
            ->field('t.id,t.type_id,t.version,t.issue_time,t.intro,type.name as typename')
            ->order('id desc')
            ->paginate($__page_size)->each(function ($item,$key){
                $resource = ResourceExtend::where('resource_id' ,$item['id'])->field('cloud_disk,url,password')->select();
                $item['resource_list'] = $resource;
                return $item;
            });

        return json(['success' => 1, 'list' => $list]);
    }

    public function friendList()
    {
        $__page_size = input('__page_size','');

        //没有传或者传0的时候按不分页处理
        if (!$__page_size) {
            $__page_size = 100000;
        }

        $list = \app\api\model\FriendLink::order('id desc')
            ->field('id,name,url as link')
            ->paginate($__page_size);

        return json(['success' => 1, 'list' => $list]);
    }

    public function setting()
    {
        $settings = \app\api\model\Setting::field('type,content')->select();
        return json(['success' => 1, 'list' => $settings]);
    }

    public function typeList()
    {
        $__page_size = input('__page_size',100000);
        $list = \app\api\model\Type::where('is_delete',0)
            ->order('id desc')
            ->field('id,name');

        if ($__page_size) {
            $list = $list->paginate($__page_size);
        } else {
            $list = $list->select();
        }

        return json(['success' => 1, 'list' => $list]);
    }
}
